package com.fy.oa.zc.controller;

import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.authenticate.domain.User;
import com.seeyon.ctp.common.controller.BaseController;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.portal.link.manager.*;
import com.seeyon.ctp.portal.link.webmodel.*;
import com.seeyon.ctp.portal.po.*;

//import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.servlet.ModelAndView;

public class OA_CRMController extends BaseController {
	private LinkSystemManager linkSystemManager;
	private LinkOptionManager linkOptionManager;
	private LinkSpaceManager linkSpaceManager;

	public void setLinkSystemManager(LinkSystemManager linkSystemManager) {
		this.linkSystemManager = linkSystemManager;
	}

	public void setLinkOptionManager(LinkOptionManager linkOptionManager) {
		this.linkOptionManager = linkOptionManager;
	}

	public void setLinkSpaceManager(LinkSpaceManager linkSpaceManager) {
		this.linkSpaceManager = linkSpaceManager;
	}

	public ModelAndView crm_page(HttpServletRequest request, HttpServletResponse response) throws Exception {

		// redirectURL
		ModelAndView ret = new ModelAndView("/fy_view/redirect");
		PortalLinkSpace lsp = null;
		PortalLinkSystem ls = null;
		Long linkSpaceId = Long.valueOf(request.getParameter("linkId"));
		String url = URLDecoder.decode(request.getParameter("redirectURL"), "utf-8");
		lsp = linkSpaceManager.findLinkSpaceById(linkSpaceId);
		boolean canAccess = false;
		User user = AppContext.getCurrentUser();
		if (lsp == null) {
			ls = linkSystemManager.selectLinkSystemWithOptions(linkSpaceId);
			canAccess = linkSystemManager.isUseTheSystem(user.getId(), ls.getId(), ls.getLinkCategoryId());
		} else {
			ls = linkSystemManager.selectLinkSystemWithOptions(lsp.getLinkSystemId());
			canAccess = linkSpaceManager.isUseTheLinkSpace(user.getId(), lsp.getId());
		}
		if (canAccess) {
			List<PortalLinkOptionVO> optionVos = getLinkOptionVO(ls, user.getId());
			for (int i = 0, size = optionVos.size(); i < size; i++) {
				ret.addObject(optionVos.get(i).getOption().getParamSign(), optionVos.get(i).getValue().getValue());
			}
		}
		ret.addObject("redirectURL", url);
		return ret;
	}

	private List<PortalLinkOptionVO> getLinkOptionVO(PortalLinkSystem linkSystem, long userId)
			throws BusinessException {
		List<PortalLinkOption> options = linkSystem.getLinkOptions();
		List<PortalLinkOptionVO> optionVos = new ArrayList<PortalLinkOptionVO>();
		if (CollectionUtils.isNotEmpty(options)) {
			Map<Long, PortalLinkOption> linkOptionMap = new HashMap<Long, PortalLinkOption>();
			List<Long> optionIds = new ArrayList<Long>();
			PortalLinkOption lo;
			for (Iterator<PortalLinkOption> iterator = options.iterator(); iterator.hasNext(); linkOptionMap
					.put(lo.getId(), lo)) {
				lo = (PortalLinkOption) iterator.next();
				optionIds.add(lo.getId());
			}
			Long optionId;
			List<PortalLinkOptionValue> plovs = linkOptionManager.selectLinkOptionValues(optionIds, userId);
			for (Iterator<PortalLinkOptionValue> iterator1 = plovs.iterator(); iterator1.hasNext(); optionIds
					.remove(optionId)) {
				PortalLinkOptionValue plov = iterator1.next();
				optionId = plov.getLinkOptionId();
				PortalLinkOption plo = (PortalLinkOption) linkOptionMap.get(optionId);
				PortalLinkOptionVO vo = new PortalLinkOptionVO(plo, plov);
				optionVos.add(vo);
			}

			PortalLinkOptionVO vo;
			for (Iterator<Long> iterator2 = optionIds.iterator(); iterator2.hasNext(); optionVos.add(vo)) {
				optionId = iterator2.next();
				PortalLinkOption plo = (PortalLinkOption) linkOptionMap.get(optionId);
				PortalLinkOptionValue plov = new PortalLinkOptionValue();
				plov.setValue(plo.getParamValue());
				vo = new PortalLinkOptionVO(plo, plov);
			}
		}
		return optionVos;
	}

//	public static void main(String[] args) {
//		try {
//			System.out.println(java.net.URLEncoder.encode("http://10.112.0.112:7777/sales_chs/start.swe?SWECmd=GotoView&SWEView=Account+List+View&SWERF=1&SWEHo=10.112.0.200&SWEBU=1&SWEApplet0=SIS+Account+List+Applet&SWERowId0=1-R2BO ","utf-8"));
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
}
